package com.david.mydemo.ctl_es.config;

import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;


@Configuration
public class EsConfig {

    @Value("${elasticsearch.uris}")
    private String[] uris;

    private String uri = "http://192.168.124.22:9200";

    private static int connectTimeOut = 5000; // 连接超时时间
    private static int socketTimeOut = 30000; // 连接超时时间
    private static int connectionRequestTimeOut = 2000; // 获取连接的超时时间
    private static int maxConnectNum = 200; // 最大连接数
    private static int maxConnectPerRoute = 200; // 最大路由连接数

    /**
     * 获取集群客户端对象
     * @return
     */
    @Bean
    public RestHighLevelClient esClusterClient() {
        // 创建多个HttpHost
        HttpHost[] httpHosts = Arrays.stream(uris).map(HttpHost::create).toArray(HttpHost[]::new);
        RestClientBuilder builder = RestClient.builder(httpHosts);
        // 异步httpclient连接延时配置
        builder.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
            @Override
            public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder builder) {
                builder.setConnectTimeout(connectTimeOut);
                builder.setSocketTimeout(socketTimeOut);
                builder.setConnectionRequestTimeout(connectionRequestTimeOut);
                return builder;
            }
        });
        // 异步httpclient连接数配置
        builder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
            @Override
            public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                httpClientBuilder.setMaxConnTotal(maxConnectNum);
                httpClientBuilder.setMaxConnPerRoute(maxConnectPerRoute);
                return httpClientBuilder;
            }
        });
        return new RestHighLevelClient(builder);
    }

    /**
     * 获取单节点客户端对象
     * @return
     */
    @Bean
    public RestHighLevelClient esSingleClient() {
        // 创建RestClientBuilder
        RestClientBuilder builder = RestClient.builder(HttpHost.create(uri));
//        // 异步httpclient连接延时配置
//        builder.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
//            @Override
//            public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder builder) {
//                builder.setConnectTimeout(connectTimeOut);
//                builder.setSocketTimeout(socketTimeOut);
//                builder.setConnectionRequestTimeout(connectionRequestTimeOut);
//                return builder;
//            }
//        });
//        // 异步httpclient连接数配置
//        builder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
//            @Override
//            public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
//                httpClientBuilder.setMaxConnTotal(maxConnectNum);
//                httpClientBuilder.setMaxConnPerRoute(maxConnectPerRoute);
//                return httpClientBuilder;
//            }
//        });
        return new RestHighLevelClient(builder);
    }


}
